In the document, we will demonstrate the effect of label switching on model fitting, different ways to mitigate or eliminate this effect, goodness of fit test and model seletion methods.
Label switching usually happens during the fitting of mixture models whose components are overlapping. We use the following example to show the effect of label switching.
library(sppmix)
int_surf <- normmix(ps = c(.3, .3, .4),
mus = list(c(0.2, 0.2), c(0.3, 0.5), c(.6, .5)),
sigmas = list(.01*diag(2), .01*diag(2), .01*diag(2)),
lambda = 100,
win = square(1))The intensity surface of the truth model shows that among the three components, two of them are overlapping.
plot(int_surf, main = "Intensity surface for the true model")You must enable Javascript to view this page properly.
Generate a point pattern from this model and fit this pattern by using est_mix_damcmc().
pp <- rsppmix(int_surf)
plot(pp, int_surf$mus)fit <- est_mix_damcmc(pp, m=3, truncate = TRUE)## 0 points are outside W=[0,1]x[0,1]
## Dataset has 95 points
## Preliminaries done. Starting MCMC...
Get the posterior mean after burnin and plot the estimated intensity surface.
postmean <- get_post(fit)
plot(postmean, main = "Posterior intensity surface")
plotmix_2d(postmean, fit$data)You must enable Javascript to view this page properly.
The plots of intensity surface shows that all components are in the center of the domain. The model doesn’t fitted well.
We can detect label switching by using plot_chains(), i.e. trace plot for the MCMC chain of \(\mu\) and p.
plot_chains(fit)The plots of chains show sudden changes of the parameter values, which indicate label switching. Also, we can use test_labswitch() to detect label switching.
test_labswitch(fit)##
## Checking for label switching...
## Label switching present.
## Permute the labels to get a better fit,
##
## or obtain the average of the surfaces
##
## [1] TRUE
After label swithcing is detected, we can use FixLS_da() function to mitigate the effect of label switching.
fit2 <- FixLS_da(fit)
postmean2 <- get_post(fit2)
plot(postmean2, main = "Posterior intensity surface after fixing label switching")
plotmix_2d(postmean2, pattern = fit$data)You must enable Javascript to view this page properly.
Also, to get the intensity surface, we can use function plot_avgsurf() to plot the average of posterior surfaces, which will not be affected by label switching.
plot_avgsurf(fit)You must enable Javascript to view this page properly.
Function mc_gof() can be used for goodness of fit test. mc_gof() uses Monte Carlo realizations of pattern patterns to simulated the distribution of test statistic and conducts the T-test.
gofts <- mc_gof(pp, postmean2, alpha = 0.05)## Dataset has 95 points
## Preliminaries done. Starting MCMC...
##
## Monte Carlo Goodness of Fit Test
## Test Statistic: 0.0748 Critical Value: 0.2014
## Null Hypothesis: Mixture Model fits the pattern well.
## Alternative: Mixture Model is not appropriate.
## p-value: 1 Large p-value, Mixture Model fits the data well.
gofts## NULL
We can use function selectMix() to select best number of components and also fit the model by using the best number of component. Notice, all possibel numbers of components need to be specified.
best_fit <- selectMix(pp, 1:5)best_fit## $AIC
## [1] -130.6623 -158.8266 -147.3058 -134.9443 -122.1185
##
## $BIC
## [1] -115.33904 -128.18003 -101.33597 -73.65130 -45.50216
##
## $ICLC
## [1] -115.33904 -88.96150 -22.47511 39.82253 96.86131
##
## $BayesFactor
## numerator 1 numerator 2 numerator 3 numerator 4
## denominator 1 1.000000e+00 7.396792e+08 8.518634e+08 1.045976e+09
## denominator 2 1.351937e-09 1.000000e+00 1.151666e+00 1.414094e+00
## denominator 3 1.173897e-09 8.683073e-01 1.000000e+00 1.227868e+00
## denominator 4 9.560447e-10 7.071664e-01 8.144195e-01 1.000000e+00
## denominator 5 1.013434e-09 7.496157e-01 8.633070e-01 1.060027e+00
## numerator 5
## denominator 1 9.867445e+08
## denominator 2 1.334017e+00
## denominator 3 1.158337e+00
## denominator 4 9.433718e-01
## denominator 5 1.000000e+00
##
## $Marginal
## [1] 6.042897e+31 4.469806e+40 5.147723e+40 6.320727e+40 5.962796e+40
##
## $LogLikelihood
## [1] 71.33115 91.41328 91.65288 91.47217 91.05924
The function selectMix() returns AIC, BIC and ICLC values for each model. Minimum value indicates the best model.
Another approach for model selection is to use birth-death MCMC (Stephens M.(2000)). To fit a Birth-Death MCMC, use function est_mix_bdmcmc. In this case, the parameter m is not the component of the mixture, but the maximum component in a Birth-death MCMC algorithm.
fitbd <- est_mix_bdmcmc(pp, 5)##
## Dataset has 95 points, maximum # of components requested is 5
## Preliminaries done. Starting Birth-Death MCMC
tb <- tabulate(fitbd$numcomp, fitbd$maxnumcomp)
mp <- barplot(tb,names.arg=1:fitbd$maxnumcomp,
xlab="Number of Components",ylab="Iterations",
main="Distribution of the number of components"
,ylim=c(0,1.2*max(tb)))The histogram of number of components suggests the model with 3 components is the best. Plot the posterior sufrace of model with 3 component.
modelbest <- get_post(fitbd, num_comp = which.max(tb))
plotmix_2d(modelbest, fit$data)You must enable Javascript to view this page properly.
Also, we can plot the average surfaces of birth-death MCMC.
plot(fitbd)You must enable Javascript to view this page properly.